using UnityEngine;
using System.Collections;

public class CsharpButtonInterface : MonoBehaviour
{

	public GUITexture[] actionButtons;
	public Vector2 actionButtonsOffset;

	public GUITexture[] monsterButtons;
	public Vector2 monsterBtttonsOffset;

	public GUITexture[] colorButtons;
	public Vector2 colorButtonsOffset;
	public GUITexture[] weaponButtons;
	public Vector2 weaponButtonsOffset;
	private bool  disableOtherClicks = false;
	private float disableFade = 0.0f;
	public Animation[] animationComponent;
	public string startAnimation;
	int monIndex = 0;
	public string colIndex;
	public string wpIndex;
	public CsharpCamControl csharpCamControl;
	private string animationName;

	void Start ()
	{
		animationComponent [monIndex].Play (startAnimation);
		animationComponent [1].gameObject.SetActiveRecursively (false);
		animationComponent [2].gameObject.SetActiveRecursively (false);
		animationComponent [3].gameObject.SetActiveRecursively (false);
	}

	void Update ()
	{
		bool clickLock = false;
		disableOtherClicks = false;
	
		if (Input.GetMouseButtonUp (0)) {
			disableOtherClicks = false;
		}
	
		// Action Buttons
		for (int i = 0; i < actionButtons.Length; i++) {
			actionButtons [i].pixelInset = new Rect (actionButtons [0].pixelInset.x + (i % 2) * (actionButtonsOffset.x + actionButtons [i].pixelInset.width),
			                                       actionButtons [0].pixelInset.y - (i / 2) * (actionButtonsOffset.y + actionButtons [i].pixelInset.height),
			                                       actionButtons [i].pixelInset.width, actionButtons [i].pixelInset.height);
			

//			actionButtons[i].pixelInset.x = actionButtons[0].pixelInset.x + (i % 2) * (actionButtonsOffset.x + actionButtons[i].pixelInset.width);
//		actionButtons[i].pixelInset.y = actionButtons[0].pixelInset.y - (i / 2) * (actionButtonsOffset.y + actionButtons[i].pixelInset.height);
//	
			if (!clickLock && csharpCamControl.CanIClick () && actionButtons [i].HitTest (Input.mousePosition)) {
				actionButtons [i].transform.localScale = new Vector3 (0.01f, 0.01f, 0);
				clickLock = true;
				disableOtherClicks = true;

//                //实验移动,蛋疼~
//				animationComponent [monIndex].transform.position += Vector3.forward * Time.deltaTime;
				if (Input.GetMouseButton (0)) {
					actionButtons [i].transform.localScale = new Vector3 (-0.01f, -0.01f, 0);
					animationName = actionButtons [i].GetComponent<CsharpStringHold> ().setString;
					Debug.Log (animationName);
					Debug.Log (animationComponent [monIndex].name);
					animationComponent [monIndex].CrossFade (animationName);
				
				}
			} else {
				actionButtons [i].transform.localScale = new Vector3 (0, 0);
			}
		}
	
		// Monster Buttons
	for (int j = 0; j < monsterButtons.Length; j++)
	{
//		monsterButtons[j].pixelInset.x = monsterButtons[0].pixelInset.x + (j % 1) * (monsterBtttonsOffset.x + monsterButtons[j].pixelInset.width);
//		monsterButtons[j].pixelInset.y = monsterButtons[0].pixelInset.y - (j / 1) * (monsterBtttonsOffset.y + monsterButtons[j].pixelInset.height);
			monsterButtons[j].pixelInset = new Rect(monsterButtons[0].pixelInset.x + (j % 1) * (monsterBtttonsOffset.x + monsterButtons[j].pixelInset.width),
			                                        monsterButtons[0].pixelInset.y - (j / 1) * (monsterBtttonsOffset.y + monsterButtons[j].pixelInset.height),
			                                        monsterButtons[j].pixelInset.width,monsterButtons[j].pixelInset.height);

		if(!clickLock && csharpCamControl.CanIClick() && monsterButtons[j].HitTest(Input.mousePosition))
		{
			monsterButtons[j].transform.localScale =new Vector3(0.02f,0.02f,0);
			clickLock = true;
			disableOtherClicks =true;
			
			if(Input.GetMouseButton(0))
			{
				monsterButtons[j].transform.localScale =new Vector3(-0.01f,-0.01f,0);
				animationName = monsterButtons[j].GetComponent<CsharpStringHold>().setString;
                    animationComponent[monIndex].CrossFade(startAnimation);
				
				switch(animationName){
					case "OrcWrrior":
						monIndex = 0;
						colIndex = "Color1";
						wpIndex = "Axe01";
						animationComponent[0].gameObject.SetActiveRecursively(true);
						animationComponent[1].gameObject.SetActiveRecursively(false);
						animationComponent[2].gameObject.SetActiveRecursively(false);
						animationComponent[3].gameObject.SetActiveRecursively(false);
						break;
						
					case "GoblinWizard":
						monIndex = 1;
						colIndex = "Color1";
						wpIndex = "Staff01";						
						animationComponent[0].gameObject.SetActiveRecursively(false);
						animationComponent[1].gameObject.SetActiveRecursively(true);
						animationComponent[2].gameObject.SetActiveRecursively(false);
						animationComponent[3].gameObject.SetActiveRecursively(false);
						break;
	
					case "OrgeHitter":
						monIndex = 2;
						colIndex = "Color1";
						wpIndex = "Blunt01";
						animationComponent[0].gameObject.SetActiveRecursively(false);
						animationComponent[1].gameObject.SetActiveRecursively(false);
						animationComponent[2].gameObject.SetActiveRecursively(true);
						animationComponent[3].gameObject.SetActiveRecursively(false);
						break;	
															
					case "TrolCurer":
						monIndex = 3;
						colIndex = "Color1";
						wpIndex = "Dagger01";
						animationComponent[0].gameObject.SetActiveRecursively(false);
						animationComponent[1].gameObject.SetActiveRecursively(false);
						animationComponent[2].gameObject.SetActiveRecursively(false);
						animationComponent[3].gameObject.SetActiveRecursively(true);
						break;
						
					default:
						monIndex = 0;
						colIndex = "Color1";
						wpIndex = "Axe01";
						animationComponent[0].gameObject.SetActiveRecursively(true);
						animationComponent[1].gameObject.SetActiveRecursively(false);
						animationComponent[2].gameObject.SetActiveRecursively(false);
						animationComponent[3].gameObject.SetActiveRecursively(false);					
						break;
				}
			}
		}
		else
		{
			monsterButtons[j].transform.localScale = new Vector3(0,0);
		}
	}
		// Color Buttons
		for (int k = 0; k < colorButtons.Length; k++) {
//		colorButtons[k].pixelInset.x = colorButtons[0].pixelInset.x + (k % 1) * (colorButtonsOffset.x + colorButtons[k].pixelInset.width);
//		colorButtons[k].pixelInset.y = colorButtons[0].pixelInset.y - (k / 1) * (colorButtonsOffset.y + colorButtons[k].pixelInset.height);
			colorButtons [k].pixelInset = new Rect (colorButtons [0].pixelInset.x + (k % 1) * (colorButtonsOffset.x + colorButtons [k].pixelInset.width),
			                                         colorButtons [0].pixelInset.y - (k / 1) * (colorButtonsOffset.y + colorButtons [k].pixelInset.height),
			                                         colorButtons [k].pixelInset.width, colorButtons [k].pixelInset.height);
			if (!clickLock && csharpCamControl.CanIClick () && colorButtons [k].HitTest (Input.mousePosition)) {
				colorButtons [k].transform.localScale = new Vector3 (0.01f, 0.01f, 0);
				clickLock = true;
				disableOtherClicks = true;

				if (Input.GetMouseButton (0)) {
					colorButtons [k].transform.localScale = new Vector3 (-0.01f, -0.01f, 0);
					colIndex = colorButtons [k].GetComponent<CsharpStringHold> ().setString;

				}
			} else {
				colorButtons [k].transform.localScale = new Vector3 (0, 0);
			}
		}
	
		// Weapon Buttons
		for (int l = 0; l < weaponButtons.Length; l++) {
//		weaponButtons[l].pixelInset.x = weaponButtons[0].pixelInset.x + (l % 2) * (weaponButtonsOffset.x + weaponButtons[l].pixelInset.width);
//		weaponButtons[l].pixelInset.y = weaponButtons[0].pixelInset.y - (l / 2) * (weaponButtonsOffset.y + weaponButtons[l].pixelInset.height);
			weaponButtons [l].pixelInset = new Rect (weaponButtons [0].pixelInset.x + (l % 2) * (weaponButtonsOffset.x + weaponButtons [l].pixelInset.width),
			                                         weaponButtons [0].pixelInset.y - (l / 2) * (weaponButtonsOffset.y + weaponButtons [l].pixelInset.height),
			                                       weaponButtons [l].pixelInset.width, weaponButtons [l].pixelInset.height);
			if (!clickLock && csharpCamControl.CanIClick () && weaponButtons [l].HitTest (Input.mousePosition)) {
				weaponButtons [l].transform.localScale =new Vector3 (0.01f, 0.01f, 0);
				clickLock = true;
				disableOtherClicks = true;

				if (Input.GetMouseButton (0)) {
					weaponButtons [l].transform.localScale =new Vector3 (-0.01f, -0.01f, 0);
					wpIndex = weaponButtons [l].GetComponent<CsharpStringHold> ().setString;
				}
			} else {
				weaponButtons [l].transform.localScale = new Vector3(0,0);
			}
		}	
	
	}
		
	public bool CanIClick ()
	{
		return !disableOtherClicks;
	}
}